package com.text.day1_5.homework;

public class UserAct {
    User[] users = new User[1];
    int length = 0;      //数组长度
    public UserAct(){
        users[0] = new User(null,null);
    }
    /**
     * 注册
     * @param userName  用户名
     * @param passWord  密码
     * @return 0    成功
     */
    public int signUp(String userName, String passWord){
        if (this.length == 0){
            this.length++;
            this.users = new User[this.length];
        } else {
            for (User user : this.users) {
                if (userName.equals(user.getUserName())){         //等会看是否报错
                    return 1;    //重复了
                }
            }
            this.length++;
            // 拷贝
            User[] users = new User[this.length];
            for (int i = 0; i < this.users.length; i++) {    //自带函数不确定是否可行
                users[i] = this.users[i];
            }
            this.users = users;   //传址
        }
        this.users[this.length - 1] = new User(userName,passWord);
        return 0;    //成功
    }

    /**
     * 登录
     * @param userName
     * @param passWord
     * @return
     */
    public int logIn(String userName, String passWord){
        for (User user : this.users) {
            if (userName.equals(user.getUserName())){
                if (user.getPassWord().equals(passWord)){
                    if (user.isFlag())
                        return 3;
                    user.setFlag(true);
                    return 0;   //成功
                } else
                    return 1;   //密码错误
            }
        }
        return 2;//用户名错误
    }

    /**
     * 退出
     * @param userName
     * @return
     */
    public int logOut(String userName){
        for (User user : this.users) {
            if (userName.equals(user.getUserName())){
                if (user.isFlag()){
                    user.setFlag(false);
                    return 0;   //成功
                } else
                    return 1;   //没登
            }
        }
        return 2;   //无此用户
    }
}
